package com.forum.daoImpl;

import org.springframework.stereotype.Component;

import com.forum.dao.UserDAO;
import com.forum.pojo.User;
import com.forum.util.BaseDAOImpl;

@Component("userDAO")
public class UserDAOImpl extends BaseDAOImpl implements UserDAO{
	/**
	 * 用户登录
	 */
	public User queryUser(String usernick, String userPasswd) throws Exception {
		String queryString = "from User user where user.userName='"+usernick+ "'" + " and user.userpass='"+userPasswd+"'";		
		try {
			return (User)getHibernateTemplate().find(queryString).get(0);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	/**
	 * 增加用户
	 */
	public int addUser(User user) throws Exception {
		// TODO Auto-generated method stub
		int num;
		try {
			num = (Integer) getHibernateTemplate().save(user);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return 0;
		}
		return num;
	}
	/**
	 * 验证名字是否可用
	 */
	public boolean validateName(String username) throws Exception {
		try {
			String queryString = "from User where userName = '"+username+"'";
			User user = (User)getHibernateTemplate().find(queryString).get(0);
			if(!user.equals(null)){
				return true;
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
		return false;
	}
	/**
	 * 根据用户Id查找用户
	 */
	public User findUserById(int userId) throws Exception {
		// TODO Auto-generated method stub
		try {
			String queryString = "from User where userId = '"+userId+"'";
			User user = (User)getHibernateTemplate().find(queryString).get(0);
			return user;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	/**
	 * 更新和完善用户信息
	 */
	public boolean updateUserInfo(User user) throws Exception {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(user);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return false;
		}
	}
	
}
